﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using YArchitech.Revit.Electric.Common;
using HYElectricCommon;

namespace Model
{
	public partial class DlgDrawRiserBridge : System.Windows.Forms.Form
	{
		public int SelectionMethod { get; set; }

		public DlgDrawRiserBridge(Document m_doc, CableTray cableTray)
		{
			this.InitializeComponent();
			this.doc = m_doc;
			this.cableTrayOld = cableTray;
			this.Bottom = Convert.ToDouble(this.txtBottom.Text);
			this.Top = Convert.ToDouble(this.txtTop.Text);
			this.xmlHelper.LoadXml(this.path, "CableTraySize");
			this.SetCableTrayType();
			this.SetSystemType();
			this.SetCableTraySize();
			this.xmlOperate.LoadXml(this.tempDataPath, "Root");
			this.LoadDlgData();
		}

		public DlgDrawRiserBridge(Document m_doc)
		{
			this.InitializeComponent();
			this.doc = m_doc;
			this.Bottom = Convert.ToDouble(this.txtBottom.Text);
			this.Top = Convert.ToDouble(this.txtTop.Text);
			this.xmlHelper.LoadXml(this.path, "CableTraySize");
			this.SetCableTrayType();
			this.SetSystemType();
			this.SetCableTraySize();
			this.xmlOperate.LoadXml(this.tempDataPath, "Root");
			this.LoadDlgData();
		}

		private void DlgDrawRiserBridge_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
		}

		public new double Bottom { get; set; }

		public new double Top { get; set; }

		private void SetCableTraySize()
		{
			foreach (XmlElement xmlElement in this.xmlHelper.GetElements(Path.Combine(new string[]
			{
				"CableTraySize"
			})))
			{
				double num = Convert.ToDouble(xmlElement.InnerText);
				this.cmbWidth.Items.Add(num);
				this.cmbHeight.Items.Add(num);
			}
			if (this.cableTrayOld != null)
			{
				double num2 = Assist.AssistFunc.feetToMM(this.cableTrayOld.Width);
				double num3 = Assist.AssistFunc.feetToMM(this.cableTrayOld.Height);
				this.cmbWidth.Text = num2.ToString();
				this.cmbHeight.Text = num3.ToString();
				double num4 = Assist.AssistFunc.feetToMM(this.cableTrayOld.LevelOffset) / 1000.0;
				this.txtBottom.Text = num4.ToString("0.00");
				this.txtTop.Text = (Convert.ToDouble(this.txtBottom.Text) + 4.0).ToString("0.00");
				this.Bottom = num4;
				this.Top = num4 + 4.0;
				return;
			}
			this.cmbWidth.Text = "300";
			this.cmbHeight.Text = "100";
		}

		private void SetCableTrayType()
		{
			IList<Element> list = RevitFilter.GetElementsOfType(this.doc, typeof(CableTrayType)).ToElements();
			if (list.Count > 0)
			{
				this.cmbCableTrayType.DataSource = list;
				this.cmbCableTrayType.DisplayMember = "Name";
			}
			if (this.cableTrayOld != null)
			{
				Element element = this.doc.GetElement(this.cableTrayOld.GetTypeId());
				for (int i = 0; i < this.cmbCableTrayType.Items.Count; i++)
				{
					if (element.Name == ((Element)this.cmbCableTrayType.Items[i]).Name)
					{
						this.cmbCableTrayType.SelectedIndex = i;
						break;
					}
				}
				if (this.cmbCableTrayType.SelectedIndex == -1)
				{
					this.cmbCableTrayType.SelectedIndex = 0;
					return;
				}
			}
			else if (this.cmbCableTrayType.Items.Count > 0)
			{
				this.cmbCableTrayType.SelectedIndex = 0;
			}
		}

		private void SetSystemType()
		{
			string text = null;
			this.cmbSystemType.Items.Add(SystemType.强电);
			this.cmbSystemType.Items.Add(SystemType.弱电);
			this.cmbSystemType.Items.Add(SystemType.消防强电);
			this.cmbSystemType.Items.Add(SystemType.消防弱电);
			try
			{
				if (this.cableTrayOld == null)
				{
					text = SystemType.强电.ToString();
				}
				else
				{
					string text2 = this.cableTrayOld.get_Parameter(BuiltInParameter.RBS_CTC_SERVICE_TYPE).AsString();
					if (string.IsNullOrEmpty(text2))
					{
						this.xmlOperate.SelectNode(Path.Combine("Root", "HYDrawRiserBridge", "Params"));
						string attribute = this.xmlOperate.GetAttribute("SystemType");
						if (!string.IsNullOrEmpty(attribute))
						{
							text = attribute;
						}
						else
						{
							text = SystemType.强电.ToString();
						}
					}
					else
					{
						text = text2;
					}
				}
			}
			catch (Exception)
			{
				text = SystemType.强电.ToString();
			}
			this.cmbSystemType.Text = text;
		}

		private void SaveDlgData()
		{
			this.xmlOperate.SelectNode(Path.Combine("Root", "HYDrawRiserBridge"));
			this.xmlOperate.SelectNode(Path.Combine("Root", "HYDrawRiserBridge", "Params"));
			this.xmlOperate.SetAttribute("CableTrayType", this.cmbCableTrayType.SelectedIndex.ToString());
			this.xmlOperate.SetAttribute("SystemType", this.cmbSystemType.Text);
			this.xmlOperate.SetAttribute("Width", this.cmbWidth.Text);
			this.xmlOperate.SetAttribute("Height", this.cmbHeight.Text);
			this.xmlOperate.SetAttribute("Bottom", this.txtBottom.Text.Trim());
			this.xmlOperate.SetAttribute("Top", this.txtTop.Text.Trim());
			string value = "0";
			if (this.rdoBehind.Checked)
			{
				value = "1";
			}
			else if (this.rdoLeft.Checked)
			{
				value = "2";
			}
			else if (this.rdoFront.Checked)
			{
				value = "3";
			}
			else if (this.rdoAngle.Checked)
			{
				value = "4";
			}
			this.xmlOperate.SetAttribute("Angle", value);
			this.xmlOperate.SetAttribute("AngleValue", this.hytxtAngle.Text.Trim());
			this.xmlOperate.SaveXml("");
		}

		private void LoadDlgData()
		{
			this.xmlOperate.SelectNode(Path.Combine("Root", "HYDrawRiserBridge", "Params"));
			if (this.cableTrayOld == null)
			{
				string attribute = this.xmlOperate.GetAttribute("CableTrayType");
				if (!string.IsNullOrEmpty(attribute))
				{
					int num = Convert.ToInt32(attribute);
					if (this.cmbCableTrayType.Items.Count > num)
					{
						this.cmbCableTrayType.SelectedIndex = num;
					}
				}
				attribute = this.xmlOperate.GetAttribute("SystemType");
				if (!string.IsNullOrEmpty(attribute))
				{
					this.cmbSystemType.Text = attribute;
				}
				attribute = this.xmlOperate.GetAttribute("Width");
				if (!string.IsNullOrEmpty(attribute))
				{
					this.cmbWidth.Text = attribute;
				}
				attribute = this.xmlOperate.GetAttribute("Height");
				if (!string.IsNullOrEmpty(attribute))
				{
					this.cmbHeight.Text = attribute;
				}
				attribute = this.xmlOperate.GetAttribute("Bottom");
				if (!string.IsNullOrEmpty(attribute))
				{
					this.txtBottom.Text = attribute;
				}
				attribute = this.xmlOperate.GetAttribute("Top");
				if (!string.IsNullOrEmpty(attribute))
				{
					this.txtTop.Text = attribute;
				}
			}
			string attribute2 = this.xmlOperate.GetAttribute("Angle");
			if (!string.IsNullOrEmpty(attribute2))
			{
				if (attribute2 == "0")
				{
					this.rdoRight.Checked = true;
				}
				else if (attribute2 == "1")
				{
					this.rdoBehind.Checked = true;
				}
				else if (attribute2 == "2")
				{
					this.rdoLeft.Checked = true;
				}
				else if (attribute2 == "3")
				{
					this.rdoFront.Checked = true;
				}
				else if (attribute2 == "4")
				{
					this.rdoAngle.Checked = true;
				}
			}
			string attribute3 = this.xmlOperate.GetAttribute("AngleValue");
			if (!string.IsNullOrEmpty(attribute3))
			{
				this.hytxtAngle.Text = attribute3;
			}
		}

		private void TextChangedValidate(object sender, string value)
		{
            System.Windows.Forms.TextBox textBox = (System.Windows.Forms.TextBox)sender;
			if (textBox.Text.Length == 0)
			{
				TaskDialog.Show("盈建科提示", "参数不能为空。");
				textBox.Text = value;
				textBox.SelectAll();
			}
			if (textBox.Name == "txtBottom")
			{
				this.Bottom = Convert.ToDouble(this.txtBottom.Text);
				return;
			}
			if (textBox.Name == "txtTop")
			{
				this.Top = Convert.ToDouble(this.txtTop.Text);
			}
		}

		private void txtBottom_TextChanged(object sender, EventArgs e)
		{
			this.TextChangedValidate(sender, "0");
		}

		private void txtTop_TextChanged(object sender, EventArgs e)
		{
			this.TextChangedValidate(sender, "4");
		}

		private void cmbWidth_TextChanged(object sender, EventArgs e)
		{
			if (this.cmbWidth.Text.Length == 0)
			{
				this.cmbWidth.Text = "300";
			}
		}

		private void cmbHeight_TextChanged(object sender, EventArgs e)
		{
			if (this.cmbHeight.Text.Length == 0)
			{
				this.cmbHeight.Text = "100";
			}
		}

		private void txtBottom_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsDouble(e, this.txtBottom);
		}

		private void txtTop_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsDouble(e, this.txtTop);
		}

		private void cmbWidth_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsPlusDouble(e, this.cmbWidth.Text);
		}

		private void cmbHeight_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsPlusDouble(e, this.cmbWidth.Text);
		}

        public void IsDouble(KeyPressEventArgs e, System.Windows.Forms.Control ctl)
		{
            System.Windows.Forms.TextBox textBox = null;
            if (ctl is System.Windows.Forms.TextBox)
			{
                textBox = (ctl as System.Windows.Forms.TextBox);
			}
			if (textBox != null)
			{
				if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '-' && e.KeyChar != '.')
				{
					e.Handled = true;
				}
				if (e.KeyChar == '-' && (textBox.SelectionStart != 0 || textBox.Text.IndexOf("-") >= 0))
				{
					e.Handled = true;
				}
				if (e.KeyChar == '.' && textBox.Text.IndexOf(".") >= 0)
				{
					e.Handled = true;
				}
			}
		}

		public void IsPlusDouble(KeyPressEventArgs e, string txt)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && txt.IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private void DlgDrawRiserBridge_FormClosing(object sender, FormClosingEventArgs e)
		{
            if (((System.Windows.Forms.Form)sender).DialogResult == DialogResult.OK)
			{
				this.SaveDlgData();
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.SelectionMethod = 1;
		}

		private void btnSel_Click(object sender, EventArgs e)
		{
			this.SelectionMethod = 0;
			base.DialogResult = DialogResult.OK;
		}

		private void hytxtAngle_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && this.hytxtAngle.Text.Trim().IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private void rdoAngle_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoAngle.Checked)
			{
				this.hytxtAngle.Enabled = true;
				this.btnSel.Enabled = true;
				return;
			}
			this.hytxtAngle.Enabled = false;
			this.btnSel.Enabled = false;
		}

		private Document doc;

		private CableTray cableTrayOld;

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = HYElectricCommon.AssistFunc.GetAppRootPath() + "\\Data\\ElectricalData\\ElectricalCableTraySize.xml";

		private const string Root = "CableTraySize";

		private const string Node = "Size";

		private YArchitech.MEP.Electric.Common.XmlHelper xmlOperate = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string tempDataPath = Path.Combine(new string[]
		{
			Product.WorkingLocation + "DrawRiserBridge.xml"
		});

		private const string tempDataRoot = "Root";

		private const string tempDataNode = "HYDrawRiserBridge";

		private const string tempDataChildNode = "Params";

		private const string CTType = "CableTrayType";

		private const string SysType = "SystemType";

		private new const string Width = "Width";

		private new const string Height = "Height";

		private const string CableTrayBottom = "Bottom";

		private const string CableTrayTop = "Top";

		private const string Angle = "Angle";

		private const string AngleValue = "AngleValue";
	}
}
